package com.imut.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imut.oa.common.ErrorCode;
import com.imut.oa.common.PageResult;
import com.imut.oa.exception.BusinessException;
import com.imut.oa.mapper.DepartmentMapper;
import com.imut.oa.mapper.EmployeeMapper;
import com.imut.oa.pojo.dto.EmployeePageQueryDTO;
import com.imut.oa.pojo.dto.UpdateEmoployeeDTO;
import com.imut.oa.pojo.entity.Departments;
import com.imut.oa.pojo.entity.User;
import com.imut.oa.pojo.vo.EmployeeVO;
import com.imut.oa.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, User>
        implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageResult getEmployeePage(EmployeePageQueryDTO employeePageQueryDTO) {
        //创建条件对象，不设置任何条件，相当于查询所以
        Page<EmployeeVO> page = new Page<>(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        // 调用自定义的联表分页查询
        Page<EmployeeVO> voPage = employeeMapper.selectEmployeePage(page, employeePageQueryDTO);
        System.out.println(voPage);
        return new PageResult(voPage.getTotal(), voPage.getRecords());
    }

    @Override
    public void disableEnablingEmployee(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户id不能为空");
        }
        User user = employeeMapper.selectById(id);
        if (user.getRole() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"不能禁用管理员");
        }

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("userStatus",user.getUserStatus() == 1 ? 0 : 1);
        employeeMapper.update(updateWrapper);
    }

    @Override
    public void updateEmoployee(UpdateEmoployeeDTO dto) {
        if (dto.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"员工id不能为空");
        }
        Departments departments = Departments.builder()
                .departmentName(dto.getDepartmentName())
                .build();

        Departments byDepartment = departmentMapper.getByDepartment(departments);
        Long id = byDepartment.getId();
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门名称不能为空");
        }
        System.out.println(dto.getGender());
        User build = User.builder()
                .id(dto.getId())
                .userName(dto.getUserName())
                .gender(dto.getGender())
                .phone(dto.getPhone())
                .email(dto.getEmail())
                .departmentId(id)
                .build();

        employeeMapper.updateEmoployee(build);
    }

    @Override
    public List<User> getAllEmployee() {
        return employeeMapper.getAllEmployee();
    }
}
